package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.adapter.CountrySortAdapter;
import com.itboye.textileexpes.beans.CountrySortModel;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.util.CharacterParserUtil;
import com.itboye.textileexpres.util.GetCountryNameSort;
import com.itboye.textileexpres.util.SPContants;
import com.itboye.textileexpres.util.SPUtils;

/**
 * 
 * ���Ҫ����
 * 
 * <p>
 * ����ϸ����
 * </p>
 * 
 * @author duanbokan
 * 
 */

public class CountryActivity extends BaseActivity {
	String TAG = "CountryActivity";

	private List<CountrySortModel> mAllCountryList;

	private ListView country_lv_countryList;

	private CountrySortAdapter adapter;

	private GetCountryNameSort countryChangeUtil;

	private CharacterParserUtil characterParserUtil;

	private ImageView back;
	private TextView title;

	private RelativeLayout relypoutGps;
	private static final int BEGIN_CODE = 0;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.coogame_country;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		initViewTwo();
		getCountryList();
	}

	/**
	 * ��ʼ������
	 */
	private void initViewTwo() {
		country_lv_countryList = (ListView) findViewById(R.id.country_lv_list);

		mAllCountryList = new ArrayList<CountrySortModel>();
		countryChangeUtil = new GetCountryNameSort();
		characterParserUtil = new CharacterParserUtil();

		adapter = new CountrySortAdapter(this, mAllCountryList);
		country_lv_countryList.setAdapter(adapter);

		relypoutGps = (RelativeLayout) findViewById(R.id.relypoutGps);
		relypoutGps.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(CountryActivity.this,
						GetProviceActivity.class);
				startActivityForResult(intent, BEGIN_CODE);
				finish();
			}
		});
		title.setText("国家地区");
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		country_lv_countryList
				.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> adapterView,
							View view, int position, long arg3) {
						String countryName = null;
						String countryNumber = null;

						// ����󷵻�
						countryName = mAllCountryList.get(position).countryName;
						countryNumber = mAllCountryList.get(position).countryNumber;

						Intent intent = new Intent();
						intent.setClass(CountryActivity.this,
								ActivityReceiptAddress.class);
						intent.putExtra("countryName", countryName);
						intent.putExtra("countryNumber", countryNumber);
						setResult(RESULT_OK, intent);
						Log.e(TAG, "countryName: + " + countryName
								+ "countryNumber: " + countryNumber);
						finish();

						SPUtils.put(CountryActivity.this, null,
								SPContants.COUNTRYNMAW, countryName);

					}
				});

	}

	/**
	 * ��ȡ�����б�
	 */
	private void getCountryList() {
		String[] countryList = getResources().getStringArray(
				R.array.country_code_list_ch);

		for (int i = 0, length = countryList.length; i < length; i++) {
			String[] country = countryList[i].split("\\*");

			String countryName = country[0];
			String countryNumber = country[1];
			String countrySortKey = characterParserUtil.getSelling(countryName);
			CountrySortModel countrySortModel = new CountrySortModel(
					countryName, countryNumber, countrySortKey);
			String sortLetter = countryChangeUtil
					.getSortLetterBySortKey(countrySortKey);
			if (sortLetter == null) {
				sortLetter = countryChangeUtil
						.getSortLetterBySortKey(countryName);
			}

			countrySortModel.sortLetters = sortLetter;
			mAllCountryList.add(countrySortModel);
		}

		adapter.updateListView(mAllCountryList);
		Log.e(TAG, "changdu" + mAllCountryList.size());
	}

}
